//
//  SDBlankPageView.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDBlankPageView.h"

#define IconViewBottomOffset 10

static UIFont * titleFont = nil;
static UIFont * subTitleFont = nil;

@interface SDBlankPageView()

@property(nonatomic,strong)UILabel* titleLabel;
@property(nonatomic,strong)UILabel* subTitleLabel;
@property(nonatomic,strong)UIImageView *subTitleImageView;
@property (nonatomic,assign) NSInteger itemCount;
@end

@implementation UIFont (category)

- (CGFloat)ttLineHeight {
    return (self.ascender - self.descender) + 1;
}

@end

@implementation SDBlankPageView

- (id)initWithView:(UIView *)view {
    
    SDBlankPageView* blankPageView = [self initWithFrame:view.bounds];
    blankPageView.containerView = view;
    return blankPageView;
}

- (id)initWithWindow:(UIWindow *)window {
    return [self initWithView:window];
}

-(id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        titleFont = [UIFont systemFontOfSize:14];
        subTitleFont = [UIFont systemFontOfSize:12];
        
        _iconView = [[UIImageView alloc] init];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_iconView];
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, titleFont.ttLineHeight)];
        _titleLabel.font = titleFont;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor colorWithRGB:0xa2a5a8];//RGB(0x43, 0x45, 0x49);
        _titleLabel.numberOfLines = 2;
        _titleLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_titleLabel];
        
        _subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width,subTitleFont.ttLineHeight * 3)];
        _subTitleLabel.font = subTitleFont;
        _subTitleLabel.textColor = RGB(0x75, 0x78, 0x7f);
        _subTitleLabel.textAlignment = NSTextAlignmentCenter;
        _subTitleLabel.numberOfLines = 3;
        _subTitleLabel.backgroundColor = [UIColor clearColor];
        
        [self addSubview:_subTitleLabel];
        
        _itemCount = 0;
        
    }
    return self;
}

- (void)setImage:(UIImage *)imgIcon withTitle:(NSString *)title withSubTitle:(NSString *)subTitle
{
    _itemCount = 0;
    
    if ( imgIcon ) {
        _iconView.hidden = NO;
        _iconView.image = imgIcon;
        [_iconView sizeToFit];
        
        // 最大 300 * 300
        if ( _iconView.width > 300 || _iconView.height > 300 ) {
            _iconView.size = CGSizeMake(300, 300);
        }
        
        _itemCount++;
    } else {
        _iconView.hidden = YES;
    }
    
    if ( title.length ) {
        _titleLabel.hidden = NO;
        
        // 主标题最多支持2行
        CGSize fSize = [title sizeWithFont:titleFont
                         constrainedToSize:CGSizeMake(self.width, CGFLOAT_MAX)
                             lineBreakMode:NSLineBreakByTruncatingTail];
        if ( fSize.height > titleFont.ttLineHeight ) {
            // 大于1行
            _titleLabel.height = floorf(titleFont.ttLineHeight * 2);
        } else {
            _titleLabel.height = floorf(titleFont.ttLineHeight);
        }
        _titleLabel.text = title;
        _itemCount++;
    } else {
        _titleLabel.hidden = YES;
    }
    
    // 副标题最多支持3行
    // 最底部，所以不用计算高度，直接给 3 行
    if ( subTitle.length ) {
        _subTitleLabel.hidden = NO;
        
        CGSize fSize = [subTitle sizeWithFont:subTitleFont
                            constrainedToSize:CGSizeMake(self.width, CGFLOAT_MAX)
                                lineBreakMode:NSLineBreakByTruncatingTail];
        if ( fSize.height > subTitleFont.ttLineHeight ) {
            // 大于1行
            _subTitleLabel.height = floorf(subTitleFont.ttLineHeight * 3);
        } else {
            _subTitleLabel.height = floorf(subTitleFont.ttLineHeight);
        }
        
        _subTitleLabel.text = subTitle;
        _itemCount++;
    } else {
        _subTitleLabel.hidden = YES;
    }
    
    [self setNeedsLayout];
}

- (void)setSubTitleImage:(UIImage*)image{
    if(image == nil){
        if(self.subTitleImageView != nil){
            [self.subTitleImageView removeFromSuperview];
            self.subTitleImageView = nil;
        }
        return;
    }
    if([_subTitleLabel.text length] == 0){
        return;
    }
    if(self.subTitleImageView == nil){
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        self.subTitleImageView = imageView;
        [self addSubview:self.subTitleImageView];
    }else{
        self.subTitleImageView.image = image;
    }
    [self.subTitleImageView sizeToFit];
    if(self.subTitleImageView.width > 30){
        self.subTitleImageView.width = 30;
    }
    if(self.subTitleImageView.height > 30){
        self.subTitleImageView.height = 30;
    }
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat gapSize = 8;
    CGFloat initialTop = 2;
    
    CGFloat totalHeight = (_iconView.hidden?0:_iconView.height) + (_titleLabel.hidden?0:_titleLabel.height) + (_subTitleLabel.hidden?0:_subTitleLabel.height) + IconViewBottomOffset;
    
    if (_itemCount > 2) {
        totalHeight += floorf((_itemCount - 2) * gapSize);
    }
    
    if ( totalHeight <= 0 ) {
        return;
    }
    
    if ( totalHeight < self.height ) {
        // 居中
        initialTop = floorf ((self.height - totalHeight) / 2.0) - 32;
    }
    
    if ( !_iconView.hidden ) {
        _iconView.frame = CGRectMake(floorf((self.width - _iconView.width)/2.0), initialTop, _iconView.width, _iconView.height);
    }
    
    if ( !_titleLabel.hidden ) {
        _titleLabel.frame = CGRectMake(0, _iconView.bottom + IconViewBottomOffset, self.width, _titleLabel.height);
    }
    
    if ( !_subTitleLabel.hidden ) {
        [_subTitleLabel sizeToFit];
        _subTitleLabel.frame = CGRectMake((self.width-_subTitleLabel.width)/2, _titleLabel.bottom + gapSize, _subTitleLabel.width, _subTitleLabel.height);
    }
    if(self.subTitleImageView != nil){
        self.subTitleImageView.center = CGPointMake(_subTitleLabel.left - 2.5f - self.subTitleImageView.width/2, _subTitleLabel.center.y);
    }
}

+(instancetype)showBlankPageViewTo:(UIView*)superview
{
    [SDBlankPageView hideBlankPageErrorFrom:superview];
    
    SDBlankPageView* blankPageView = [[self alloc] initWithView:superview];
    [superview addSubview:blankPageView];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kDidAddBlankPageSubviewNotification object:nil userInfo:nil];
    
    
    return blankPageView;
}
+(void)hideBlankPageErrorFrom:(UIView*)superview
{
    for (UIView* subView in superview.subviews) {
        if ([subView isKindOfClass:[self class]]) {
            [subView removeFromSuperview];
            
            break;
        }
    }
}
@end

